<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Com_search extends CI_Controller {
    private $per_page = 24;
    private $total_rows = 12;
    private $tb_quangcao = 'quangcao';
    function  __construct(){
        parent::__construct();      
    }
    function index(){
        $getAlias = $this->common_model->getdata('alias', array('type' => 'tintuc'));
        foreach ($getAlias as $ali) {
            $dataAliastt[$ali->a_id] = $ali;
        }
        $where = array('publish'=>1);        
        if($_POST){
            $this->session->set_userdata('keyword',$this->security->xss_clean($_POST['keyword']));
        }        
        $offset = preg_replace("/(.html)/",'', $this->security->xss_clean($this->uri->segment(2,0)));
        $keyword = $this->session->userdata('keyword');       
        if(!empty($keyword)){
            $this->db->like('title',$keyword);
        } 
        $this->db->limit($this->per_page,$offset);
        $query = $this->db->get('tintuc');       
        $dataSearch = $query->result();
        if(!empty($dataSearch)){
            if(!empty($keyword)){$this->db->like('title',$keyword);}
            $query = $this->db->get('tintuc');
            $this->total_rows = $query->num_rows();
            $this->phantrang(); 
            $data['content'] = $this->load->view('com_search/views_search',array('mNews'=> $dataSearch,'dataAliastt'=>$dataAliastt),true);
        }else{
            $thongbao = "Không tìm thấy sản phẩm nào";
            $data['content'] =$this->load->view('404',array('thongbao'=> $thongbao),true);
        $setting = $this->common_model->getone('setting',array('id'=>1));
        $data['title'] = 'Tìm kiếm';
        $data['metades'] = $setting->metades;
        $data['metakey'] = $setting->metakey;
        }
        $this->load->view('template',$data);
    }  
    
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().$this->uri->segment(1).'/';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 2;
        $config['num_links'] = 12;
        $this->pagination->initialize($config);
    }
}